/**
 * Created on 2009-4-17 下午02:56:08 <br>
 * at Tianhe Guangzhou
 */
package com.rich.component.transaction.demo.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.rich.component.transaction.demo.domain.DemoObject;

/**
 * . <br>
 * 
 * @author xdan
 */
public class TransactionTestDaoImpl extends JdbcDaoSupport implements
		TransactionTestDao {

	public void deleteDemoObject(Long id) {
		String deleteSql = "delete from demo where oid=?";
		getJdbcTemplate().update(deleteSql, new Object[] { id });

	}

	public DemoObject getDemoObject(Long id) {
		if (id <= 0) {
			return new DemoObject();
		}
		String querySql = "select * from demo where oid=?";
		DemoObject demo = (DemoObject) getJdbcTemplate().queryForObject(
				querySql, new Object[] { id }, new RowMapper() {

					public Object mapRow(ResultSet rs, int arg1)
							throws SQLException {
						DemoObject demo = new DemoObject();
						demo.setOid(rs.getLong("oid"));
						demo.setCreateTime(rs.getTimestamp("create_time"));
						demo.setContent(rs.getString("content"));
						demo.setResult(rs.getString("result"));
						demo.setRemark(rs.getString("remark"));
						return demo;
					}
				});
		return demo;
	}

	public long insertDemoObject(final DemoObject demo) {
		final long newOid = getJdbcTemplate().queryForLong(
				"select HIBERNATE_SEQUENCE.nextval from dual");

		String insertSql = "insert into demo values(?,?,?,?,?)";
		getJdbcTemplate().update(insertSql, new PreparedStatementSetter() {

			public void setValues(PreparedStatement ps) throws SQLException {
				Date createtime = demo.getCreateTime();
				ps.setLong(1, newOid);
				ps.setTimestamp(2, createtime == null ? null
						: new java.sql.Timestamp(createtime.getTime()));
				ps.setString(3, demo.getContent());
				ps.setString(4, demo.getResult());
				ps.setString(5, demo.getRemark());
			}
		});
		demo.setOid(newOid);
		return newOid;
	}

	public void updateDemoObject(final DemoObject demo) {
		String updateSql = "update demo set create_time=?,content=?,result=?,remark=? where oid=?";
		getJdbcTemplate().update(updateSql, new PreparedStatementSetter() {

			public void setValues(PreparedStatement ps) throws SQLException {
				Date createtime = demo.getCreateTime();
				ps.setTimestamp(1, createtime == null ? null
						: new java.sql.Timestamp(createtime.getTime()));
				ps.setString(2, demo.getContent());
				ps.setString(3, demo.getResult());
				ps.setString(4, demo.getRemark());
				ps.setLong(5, demo.getOid());
			}
		});
	}

	@SuppressWarnings("unchecked")
	public List<DemoObject> getDemoList(String business_type) {
		String querySql = getQuerySql();
		List<DemoObject> list = getJdbcTemplate().query(querySql,
				new Object[] {}, new RowMapper() {

					public Object mapRow(final ResultSet rs, final int arg1)
							throws SQLException {
						DemoObject demo = new DemoObject();
						demo.setOid(rs.getLong("oid"));
						demo.setCreateTime(rs.getTimestamp("create_time"));
						demo.setContent(rs.getString("content"));
						demo.setResult(rs.getString("result"));
						demo.setRemark(rs.getString("remark"));

						return demo;
					}
				});
		return list;
	}

	/**
	 * @param userId
	 * @param type
	 * @return String
	 */
	private String getQuerySql() {
		StringBuffer sbSql = new StringBuffer();
		sbSql.append("SELECT d.*  ");
		sbSql.append(" FROM demo d order by oid desc");
		// sbSql.append("SELECT d.*,t.*");
		// sbSql.append(" FROM demo d, jbpm_v_process t");
		// sbSql.append(" WHERE ");
		// sbSql.append(" t.bid = d.oid ");
		// sbSql.append(" AND t.btype = ? ");
		return sbSql.toString();
	}
}
